<?php 
	require_once("init.php");  
?>

<?php
	$qTopic = mysql_query("
    SELECT
			t.*,
			c.name AS categoryName,
			u.username AS username
		FROM topics t
			INNER JOIN categories c ON t.categoryId = c.categoryId
			INNER JOIN users u ON t.userId = u.userId
    WHERE topicId = ".$_GET["id"]."
  ");
	$strVoted = "
    SELECT *
		FROM votes
    WHERE topicId = ".$_GET["id"]."
  ";
	if (isset($_SESSION["userId"])) {
		$strVoted = $strVoted."
			AND userId = ".$_SESSION["userId"]."
		";
	} else if (strlen(session_id()) > 0) {
		$strVoted = $strVoted."
			AND sessionId = '".session_id()."'
		";
	}
	$qVoted = mysql_query($strVoted);
	
	$isVoted = false;
	if (mysql_num_rows($qVoted) > 0) { 
		$isVoted = true;
	}
	
  while ($row = mysql_fetch_array($qTopic)) {
		if ($isVoted) { 
			$totalVotes = $row["answer1Votes"] + $row["answer2Votes"];
			if ($totalVotes > 0) {
				$answer1Percent = $row["answer1Votes"]/$totalVotes * 100;
				$answer2Percent = $row["answer2Votes"]/$totalVotes * 100;
			} else {
				$answer1Percent = 0;
				$answer2Percent = 0;
			}
			$answer1Class = "lose";
			$answer2Class = "lose";
			if ($answer1Percent > $answer2Percent) {
				$answer1Class = "win";
				$answer2Class = "lose";
			} else if ($answer1Percent < $answer2Percent) {
				$answer1Class = "lose";
				$answer2Class = "win";
			} 
		}
?>
<ul id="topic-<?php print $row["topicId"]; ?>" 
		title="<?php if ($isVoted) print "Voted"; else print "Vote"; ?>" 
	<?php if (isset($_SESSION["userId"])) { ?>
		showToolbarButton="true" 
    toolbarButtonText="Report" 
    toolbarButtonHREF="reporttopic.php?id=<?php print $_GET["id"]; ?>">
  <?php } ?>
	<li class="heading">
		<?php print $row["question"]; ?><br />
    <small>
    	By <?php print $row["username"]; ?>
      &nbsp;|&nbsp; <?php print $row["categoryName"]; ?>
    	&nbsp;|&nbsp; Closed <?php print closingTimeFormat($row["dateClosed"]); ?>
    </small>
  </li>
  <?php 
		if ($isVoted) { 
			$votedRow = mysql_fetch_array($qVoted);
	?>
	<li class="<?php print $answer1Class; ?>">
  	<div class="answer">
			<?php print $row["answer1"]; ?> <small>(<?php print $row["answer1Votes"]; ?>)</small>
    	<?php if ($votedRow["answer"] == 1) { ?><img src="img/arrow-voted-<?php print $answer1Class; ?>.gif" width="65" height="15" /><?php } ?>
    </div>
    <div class="percent" style="width: <?php print $answer1Percent; ?>%;"><!-- --></div>
  </li>
	<li class="<?php print $answer2Class; if ($votedRow["answer"] == 1) print " voted"; ?>">
  	<div class="answer">
			<?php print $row["answer2"]; ?> <small>(<?php print $row["answer2Votes"]; ?>)</small>
    	<?php if ($votedRow["answer"] == 2) { ?><img src="img/arrow-voted-<?php print $answer2Class; ?>.gif" width="65" height="15" /><?php } ?>
    </div>
    <div class="percent" style="width: <?php print $answer2Percent; ?>%;"><!-- --></div>
  </li>
  <?php
		} else {
	?>
	<li id="votetopic<?php print $row["topicId"]; ?>-answer1"><a class="answer" href="javascript:get(null, 'votetopic.php?id=<?php print $row["topicId"]; ?>&a=1', votetopic_callback);" target="_self"><?php print $row["answer1"]; ?></a></li>
	<li id="votetopic<?php print $row["topicId"]; ?>-answer2"><a class="answer" href="javascript:get(null, 'votetopic.php?id=<?php print $row["topicId"]; ?>&a=2', votetopic_callback);" target="_self"><?php print $row["answer2"]; ?></a></li>
 	<li id="votedtopic<?php print $row["topicId"]; ?>-answer1" style="display: none;">
  	<div class="answer">
			<?php print $row["answer1"]; ?> <small id="votedtopic<?php print $row["topicId"]; ?>-answer1-votes"></small>
      <img id="votedtopic<?php print $row["topicId"]; ?>-answer1-voted" width="65" height="15" />
    </div>
    <div id="votedtopic<?php print $row["topicId"]; ?>-answer1-percentage" class="percent"><!-- --></div>
  </li>
 	<li id="votedtopic<?php print $row["topicId"]; ?>-answer2" style="display: none;">
  	<div class="answer">
			<?php print $row["answer2"]; ?> <small id="votedtopic<?php print $row["topicId"]; ?>-answer2-votes"></small>
      <img id="votedtopic<?php print $row["topicId"]; ?>-answer2-voted" width="65" height="15" />
    </div>
    <div id="votedtopic<?php print $row["topicId"]; ?>-answer2-percentage" class="percent"><!-- --></div>
  </li>
  <?php
		}
	?>
</ul>
<?php
	}
?>

<?php 
	require_once("end.php"); 
?>